﻿#region using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Markup;
using Microsoft.Win32;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Input;
using System.Xml;
using System.Globalization;
using System.IO;
using System.Timers;
using System.ComponentModel;
using Ab2d.Common.ReaderWmf;
using System.Windows.Forms;
using System.Collections;
using System.Windows.Media.Animation;
#endregion

namespace MultiTouchDemonstrator
{
    class MapManipulation : IObjectManipulation
    {
        public void ManipulationDelta(object container,ManipulationDeltaEventArgs e)
        {

            ManipulationDelta currentDelta = e.DeltaManipulation;
            MapArea mapArea = container as MapArea;
            // Get the Rectangle and its RenderTransform matrix.
            Canvas mapToMove = e.OriginalSource as Canvas;
            if (mapToMove != null)
            {
                Matrix mapMatrix = ((MatrixTransform)mapToMove.RenderTransform).Matrix;
                // Manipulation origin center
                Point orgCenter = e.ManipulationOrigin;
                // Map new center.
                Point center = mapMatrix.Transform(orgCenter);
                mapMatrix.ScaleAt(currentDelta.Scale.X, currentDelta.Scale.Y, orgCenter.X, orgCenter.Y);
                mapArea.ZoomCanvas = new Point(mapArea.ZoomCanvas.X * currentDelta.Scale.X, mapArea.ZoomCanvas.Y * currentDelta.Scale.Y);
                mapMatrix.RotateAt(currentDelta.Rotation, orgCenter.X, orgCenter.Y);
                mapMatrix.Translate(currentDelta.Translation.X, currentDelta.Translation.Y);
                mapToMove.RenderTransform = new MatrixTransform(mapMatrix);
                // ne marche pas pour le moment
                Rect containingRect = new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize);
                Rect shapeBounds = mapToMove.RenderTransform.TransformBounds(new Rect(mapToMove.RenderSize));
                if (e.IsInertial && !containingRect.Contains(shapeBounds))
                {
                    e.ReportBoundaryFeedback(e.DeltaManipulation);
                    e.Complete();
                }

                // transformation des symboles incrustes dans le plan  
                bool iszoomOut = (currentDelta.Scale.X < 1 && currentDelta.Scale.Y < 1);
                foreach (UIElement element in mapArea.MapCanvas.Children)
                {
                    if (element is Image)
                    {
                        Image item = element as Image;
                        if (item != null && item.Tag != null)
                        {
                            item.Height = item.ActualHeight / currentDelta.Scale.X;
                            item.Width = item.ActualWidth / currentDelta.Scale.Y;

                            if (iszoomOut)
                            {
                                //element disparait
                                if (mapArea.ZoomCanvas.X < 3 || mapArea.ZoomCanvas.Y < 3)
                                {
                                    item.Visibility = Visibility.Hidden;
                                }
                                else if (mapArea.ZoomCanvas.X < 3.5 || mapArea.ZoomCanvas.Y < 3.5)
                                {
                                    item.Height = (item.ActualHeight * currentDelta.Scale.X * 0.9);
                                    item.Width = item.ActualWidth * currentDelta.Scale.Y * 0.9;
                                }


                            }
                            else
                            {
                                //element reapparait
                                if (mapArea.ZoomCanvas.X > 3 && mapArea.ZoomCanvas.Y > 3)
                                {
                                    item.Visibility = Visibility.Visible;
                                    Point originImageSourceScale = (Point)item.Tag;
                                    double normaleSizeX = (mapArea.ZoomItem.NewValue * originImageSourceScale.X / 100) / mapArea.ZoomCanvas.X;
                                    double normaleSizeY = (mapArea.ZoomItem.NewValue * originImageSourceScale.Y / 100) / mapArea.ZoomCanvas.Y;
                                    double progressSizeX = (item.ActualHeight * currentDelta.Scale.X) / 0.9;
                                    double progressSizeY = (item.ActualWidth * currentDelta.Scale.Y) / 0.9;
                                    item.Height = progressSizeX < normaleSizeX ? progressSizeX : normaleSizeX;
                                    item.Width = progressSizeY < normaleSizeY ? progressSizeY : normaleSizeY;

                                }
                            }
                        }
                    }

                }
                e.Handled = true;
            }
        }
        public void ManipulationStarting(object sender, ManipulationStartingEventArgs e, IInputElement container, bool handled)
        {
            e.ManipulationContainer = container;
            e.Handled = handled;
        }
        public void ManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e, double translateDeceleration, double rotateDeceleration, bool handled)
        {
            e.TranslationBehavior.DesiredDeceleration = translateDeceleration;
            e.RotationBehavior.DesiredDeceleration = rotateDeceleration;
            e.Handled = handled;
        }
        public void ManipulationCompleted(object sender, ManipulationCompletedEventArgs e, UIElement container) 
        {
            container.IsManipulationEnabled = true;
        }
    }
}
